import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/2;
 * Otherwise, I don't know who created it either
 */

public class Test02 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/lanou3",
                    "root",
                    "123456");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String sql1 = "UPDATE bank " +
                "SET money = money - 5 " +
                "WHERE cname = '鸣人'";

        String sql2 = "UPDATE bank " +
                "SET money = money + 5 " +
                "WHERE cname = '佐助'";

        try {
            conn.setAutoCommit(false);//开启事务
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql1);
            statement.executeUpdate(sql2);
            conn.commit();

            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            // 在catch中进行回滚
            // 当执行sql出错了,可以触发回滚
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }
}
